function saveToken(token) {
	localStorage.accessToken = token;
}

//原生js ajax工具，无需jquery
function ajax(params) {
	var method = params.method ? params.method.toUpperCase() : 'GET';
	var async = params.async != undefined ? params.async : true; //true为异步，默认
	var url = params.url;
	if (!url) {
		throw 'url不得为空';
	}
	var data = params.data;
	if (method == 'POST') {
		data = JSON.stringify(data)
	} else {
		url += '?';
		for (key in data) {
			url += key + '=' + data[key] + '&';
		}
		url = url.substr(0, url.length - 1);
		data = null;
	}

	var xhr = new XMLHttpRequest;
	xhr.withCredentials = true;
	xhr.open(method, url, async);
	var token = localStorage.accessToken;
	xhr.setRequestHeader('Authorization', 'Bearer' + token);
	if (method == 'POST') {
		xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8')
	}
	xhr.onload = function () {
		if (xhr.status == 200) {
			var res = JSON.parse(xhr.responseText);
			params.success && params.success(res);
		} else {
			params.fail && params.fail(xhr.responseText);
		}
	}
	xhr.send(data);
}

//原生js ajax工具，无需jquery
function ajaxFile(params) {
	var async = params.async != undefined ? params.async : true; //true为异步，默认
	var url = params.url;
	if (!url) {
		throw 'url不得为空';
	}
	var data = params.data;
	var formdata = new FormData();
	for (key in data) {
		formdata.append(key, data[key]);
	}

	var xhr = new XMLHttpRequest;
	xhr.withCredentials = true;
	xhr.open('POST', url, async);
	xhr.onload = function () {
		if (xhr.status == 200) {
			var res = JSON.parse(xhr.responseText);
			params.success && params.success(res);
		} else {
			params.fail && params.fail(xhr.responseText);
		}
	}
	xhr.send(formdata);
}

//获取form中所有表单数据并转换为对象
//传入obj为 document.getElectmentById()得到的对象
function getFormObj(form, chb_split) {
	if (chb_split == undefined) {
		chb_split = ',';
	}

	var data = {};
	var inputs = form.querySelectorAll('input');
	for (i = 0; i < inputs.length; i++) {
		var input = inputs[i];
		if (!input.name || input.disabled) {
			continue;
		}
		if (input.type == 'checkbox') {
			if (typeof data[input.name] == 'undefined') {
				data[input.name] = chb_split == false ? [] : '';
			}
			if (input.checked) {
				if (chb_split == false) {
					data[input.name].push(input.value);
				} else {
					if (data[input.name] == '') {
						data[input.name] = input.value;
					} else {
						data[input.name] += chb_split + input.value;
					}
				}
			}
		}
		else if (input.type == 'radio') {
			if (typeof data[input.name] == 'undefined') {
				data[input.name] = '';
			}
			if (input.checked) {
				data[input.name] = input.value;
			}
		}
		else if (input.type == 'file') {
			continue;
		}
		else if (input.type == 'number') {
			data[input.name] = input.value === '' ? null : Number(input.value);
		}
		else {
			data[input.name] = input.value;
		}
	}
	var selects = form.querySelectorAll('select');
	for (i = 0; i < selects.length; i++) {
		var select = selects[i];
		if (select.disabled) {
			continue;
		}
		if (selects[i].multiple) {
			data[select.name] = [];
			var options = select.querySelectorAll('option:checked');
			for (j = 0; j < options.length; j++) {
				data[select.name].push(options[j].value);
			}
			data[select.name] = data[select.name].join(chb_split);
		} else {
			data[select.name] = select.value;
		}
	}
	var textareas = form.querySelectorAll('textarea');
	for (i = 0; i < textareas.length; i++) {
		var textarea = textareas[i];
		if (textarea.disabled) {
			continue;
		}
		data[textarea.name] = textarea.value;
	}
	return data;
}

// 表单自动加载数据函数，
function formload(obj, data) {
	var inputObj = obj.getElementsByTagName('input');
	for(i=0; i<inputObj.length; i++) {
		var name = inputObj[i].name;
		if(data[name] === undefined) {
			continue;
		}
		switch(inputObj[i].type) {
			case 'radio':
				if(inputObj[i].value == data[name]) {
					inputObj[i].checked = 'checked';
				}
				break;
			case 'checkbox':
				var arr = [];
				if(typeof(data[name]) == 'string') {
					arr = data[name].split(',');
				} else {
					arr = data[name];
				}
				for(index in arr) {
					if(inputObj[i].value == arr[index]) {
						inputObj[i].checked = 'checked';
						break;
					}
				}
				break;
			case 'file':
				break;
			case 'date':
				inputObj[i].value = typeof data[name]=='number' ? timestampToStr(data[name], onlyDay=true) : data[name];
				break;
			case 'datetime':
				inputObj[i].value = typeof data[name]=='number' ? timestampToStr(data[name]) : data[name];
				break;
			case 'text':
			case 'hidden':
			default :
				inputObj[i].value = data[name];
		}
	}
	var textareaObj = obj.getElementsByTagName('textarea');
	for(i=0; i<textareaObj.length; i++) {
		var name = textareaObj[i].name;
		if(data[name] === undefined || data[name] === null) {
			continue;
		}
		textareaObj[i].value = data[name];
	}
	var selectObj = obj.getElementsByTagName('select');
	for(i=0; i<selectObj.length; i++) {
		var name = selectObj[i].name;
		if(data[name] === undefined) {
			continue;
		}
		selectObj[i].value = data[name];
	}
	var imgObj = obj.getElementsByTagName('img');
	for(i=0; i<imgObj.length; i++) {
		var name = imgObj[i].name;
		if(data[name] === undefined) {
			continue;
		}
		imgObj[i].src = data[name];
	}
	var spanObj = obj.getElementsByTagName('span');
	for(i=0; i<spanObj.length; i++) {
		var name = spanObj[i].getAttribute('name');
		if(data[name] === undefined) {
			continue;
		}
		spanObj[i].innerText = data[name];
	}
}
